package com.itkaien.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itkaien.common.R;
import com.itkaien.pojo.Message;
import com.itkaien.service.MessageService;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private MessageService messageService;


    @GetMapping("/getMessage")
    public PageInfo<Message> getMessagepage(String beforeOutTime,String afterOutTime , int page, int pageSize) {

        /**
         * 设置时间mvc无法接收时间类型，需要转换
         */
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Date beforeOutTime1=null;
        Date afterOutTime1=null;
        if (beforeOutTime!=null){
            LocalDateTime dateTime = LocalDateTime.parse(beforeOutTime, formatter);
            ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.of("Asia/Shanghai"));
            beforeOutTime1 = Date.from(zonedDateTime.toInstant());
        }

       if (afterOutTime!=null){
           LocalDateTime dateTime1 = LocalDateTime.parse(afterOutTime, formatter);
           ZonedDateTime zonedDateTime1 = dateTime1.atZone(ZoneId.of("Asia/Shanghai"));
            afterOutTime1 = Date.from(zonedDateTime1.toInstant());
       }

        /**
         * 查询分页信息
         */
        int start = 0;
        start = page - 1;
        PageInfo<Message> messagePageInfo = messageService.selectMessage(beforeOutTime1, afterOutTime1, start, pageSize);
        return messagePageInfo;
    }

    @PutMapping("updatemessage")
    public R<String> updateMessage(Integer[] ids,int status) throws JsonProcessingException {
        messageService.updateMessage(ids,status);

        /**
         * 修改redis中的status为已经读取
         */
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        String value = valueOperations.get("message");
        ObjectMapper objectMapper = new ObjectMapper();
        Message message = objectMapper.readValue(value, Message.class);
        message.setReadStatus(status);
        String updatedValue = objectMapper.writeValueAsString(message);
        redisTemplate.opsForValue().set("message", updatedValue);

        return R.success("消息已经读取");
    }

    /**
     * 设置消息超过3消息自动发送
     *MessageExpirationCheckTask  实现类，短信服务并未实现 但是可以简单的yong邮箱实现，由于时间原因不做实现
     * **/
}
